<?php

require_once(SRC_PATH."Object/Url.php");
require_once(SRC_PATH."Object/Hostname.php");
require_once(RX_PATH."Date/RXDate.php");
require_once("GenericReader.php");

class HostnameReader extends GenericReader{
		
	public function __construct($object = NULL, $connection = NULL){
		parent::__construct($object, $connection);
	}
	
	public function execute(){
		$this->select = new RXSelect($this->connection);
		
		switch($this->operation){
			default:
				return $this->executeGetOne();			
		}
	}
	
	public function executeGetOne(){
		$query = "	SELECT url.*, 
							hostname.id as hostname_id, 
							hostname.hostname as hostname_name, 
							hostname.visits as hostname_visits 
							FROM url INNER JOIN hostname ON url.hostname = hostname.id 
							WHERE url.id = '".$this->object->getId()."'";
		$this->select->setQuery($query);
		return $this->select->execute();
	}
	
	public function next(){
		$row = $this->select->next();
		if($row){
			$lastAccess = new RXDate();
			$lastAccess->setUnixTime($row["last_access"]);
			return new Url(	$row["id"], 
										$row["location"], 
										new Hostname($row["hostname_id"], $row["hostname_name"], $row["hostname_visits"]), 
										$lastAccess);
		}else{
			return FALSE;
		}
	}
}